package com.jmz.apt.api.service;

import com.jmz.apt.api.Utils.ObjectUtil;
import com.jmz.apt.api.common.AptConstant;
import com.jmz.apt.api.dao.AdminDao;
import com.jmz.apt.api.entity.dbEntity.JmzAdminRole;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUser;
import com.jmz.apt.api.entity.dbEntity.JmzAdminUserRoles;
import com.jmz.apt.api.enums.ResponseCode;
import com.jmz.apt.api.exception.BusinessException;
import com.jmz.apt.api.message.request.RequestMessage;
import com.jmz.apt.api.service.user.AdminUser;
import com.jmz.apt.api.service.user.AdminUserRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * Created by fanglexue on 2019/11/3.
 */
@Service
@Slf4j
public class AdminUserService {

    @Autowired
    AdminUser adminUser;

    @Autowired
    AdminUserRole adminUserRole;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;


    public List<JmzAdminUser> getAdminUserList(RequestMessage requestMessage) {
        return adminUser.findAllAdminUsers(requestMessage);
    }

    public Integer count(RequestMessage requestMessage){
        return  100;
    }


    public JmzAdminUser getAdminInfo(Integer adminId){
        String key = AptConstant.Redis.ADMIN_USER_PREFIX + adminId;
        boolean b = stringRedisTemplate.hasKey(key).booleanValue();
        if(b){
            String  ju = stringRedisTemplate.opsForValue().get(key);
            JmzAdminUser jmzAdminUser = ObjectUtil.json2obj(ju,JmzAdminUser.class);
            return jmzAdminUser;
        }
        JmzAdminUser jmzAdminUser = adminUser.getAdUserInfo(adminId);
        if(null == jmzAdminUser){
            throw  new BusinessException(ResponseCode.GET_UINFO_FAILURE.getCode(),
                    ResponseCode.GET_UINFO_FAILURE.getDesc());
        }
        return jmzAdminUser;
    }


    @Transactional
    public Boolean saveAdminUser(List<List<JmzAdminUser>> jmzAdminUsers){
        try {
            JmzAdminUserRoles jmzAdminUserRoles = new JmzAdminUserRoles();
            if (!jmzAdminUsers.isEmpty() && jmzAdminUsers != null) {
                for (List<JmzAdminUser> lJmzAdminUser : jmzAdminUsers) {
                    if (!lJmzAdminUser.isEmpty() && lJmzAdminUser != null) {
                        batchSaveAdminUser(lJmzAdminUser);
                    }
                }
            }
        }catch (DataAccessException e){
            log.error(e.getMessage());
            throw new BusinessException(ResponseCode.DB_OPERATION_ERROR.getCode(),
                    ResponseCode.DB_OPERATION_ERROR.getDesc());
        }catch (BusinessException be){
            throw new BusinessException(be.getCode(),
                    be.getMessage());
        }
        return  true;
    }

    @Transactional
    public  Boolean updateAdminUser(List<List<JmzAdminUser>> jmzAdminUsers){
        try {
            if (!jmzAdminUsers.isEmpty() && jmzAdminUsers != null) {
                for (List<JmzAdminUser> lJmzAdminUser : jmzAdminUsers) {
                    if (!lJmzAdminUser.isEmpty() && lJmzAdminUser != null) {
                        batchUpdateAdminUser(lJmzAdminUser);
                    }
                }
            }
        }catch (DataAccessException e){
            log.error(e.getMessage());
            throw new BusinessException(ResponseCode.DB_OPERATION_ERROR.getCode(),
                    ResponseCode.DB_OPERATION_ERROR.getDesc());
        }catch (BusinessException be){
                throw new BusinessException(be.getCode(),
                    be.getMessage());
        }
        return  true;
    }


    @Transactional
    public  Boolean deleteAdminUser(List<List<JmzAdminUser>> jmzAdminUsers){
        try {
            JmzAdminUserRoles jmzAdminUserRoles = new JmzAdminUserRoles();
            if (!jmzAdminUsers.isEmpty() && jmzAdminUsers != null) {
                for (List<JmzAdminUser> lJmzAdminUser : jmzAdminUsers) {
                    if (!lJmzAdminUser.isEmpty() && lJmzAdminUser != null) {
                        batchDeleteAdminUser(lJmzAdminUser);
                    }
                }
            }
        }catch (DataAccessException e){
            log.error(e.getMessage());
            throw new BusinessException(ResponseCode.DB_OPERATION_ERROR.getCode(),
                    ResponseCode.DB_OPERATION_ERROR.getDesc());
        }catch (BusinessException be){
            throw new BusinessException(be.getCode(),
                    be.getMessage());
        }
        return  true;
    }


    public boolean changePassword(JmzAdminUser user) {
        return adminDao.changePassword(user) > 0;
    }


    private void batchUpdateAdminUser(List<JmzAdminUser> jmzAdminUsers){
        JmzAdminUserRoles jmzAdminUserRoles = new JmzAdminUserRoles();
        for (JmzAdminUser jmzAdminUser : jmzAdminUsers) {
            adminUser.updateAdminUser(jmzAdminUser);
            List<JmzAdminRole> jmzAdminRoles = jmzAdminUser.getAdminRoles();
            if (!jmzAdminRoles.isEmpty() && jmzAdminRoles != null) {
                jmzAdminUserRoles.setAdminId(jmzAdminUser.getId());
                adminUserRole.deleteBatchAdminUserRole(jmzAdminUserRoles);
                for (JmzAdminRole jmzAdminRole : jmzAdminRoles) {
                    //TODO 后续优化成单条SQL
                    //TODO 后续性能优化 利用取请求体差集和存在redis中管理者所拥有的权限来决定插入和删除避免现在的批量删除
                    jmzAdminUserRoles.setRoleId(jmzAdminRole.getId());
                    adminUserRole.saveAdminUserRoles(jmzAdminUserRoles);
                }
            }
        }
    }

    private void batchSaveAdminUser(List<JmzAdminUser> jmzAdminUsers){
        JmzAdminUserRoles jmzAdminUserRoles = new JmzAdminUserRoles();
        for (JmzAdminUser jmzAdminUser : jmzAdminUsers) {
            Integer adminId = adminUser.saveAdminUser(jmzAdminUser);
            List<JmzAdminRole> jmzAdminRoles = jmzAdminUser.getAdminRoles();
            if (!jmzAdminRoles.isEmpty() && jmzAdminRoles != null) {
                jmzAdminUserRoles.setAdminId(adminId);
                for (JmzAdminRole jmzAdminRole : jmzAdminRoles) {
                    //TODO 后续优化成单条SQL
                    jmzAdminUserRoles.setRoleId(jmzAdminRole.getId());
                    adminUserRole.saveAdminUserRoles(jmzAdminUserRoles);
                }
            }
        }
    }

    private void batchDeleteAdminUser(List<JmzAdminUser> jmzAdminUsers){
        JmzAdminUserRoles jmzAdminUserRoles = new JmzAdminUserRoles();
        for (JmzAdminUser jmzAdminUser : jmzAdminUsers) {
            adminUser.deleteAdminUser(jmzAdminUser);
            List<JmzAdminRole> jmzAdminRoles = jmzAdminUser.getAdminRoles();
            if (!jmzAdminRoles.isEmpty() && jmzAdminRoles != null) {
                jmzAdminUserRoles.setAdminId(jmzAdminUser.getId());
                adminUserRole.deleteBatchAdminUserRole(jmzAdminUserRoles);
            }
        }
    }




}
